package xml;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

import com.thuanhoang.boo.model.Product;

public class XMLProductHandler extends DefaultHandler {
	public static ArrayList<Product> itemList;
	public boolean current = false;
	public String currentValue = null;
	public Product currentProduct;

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub

		current = true;

		if (localName.equals("Result")) {
			Log.d("start", "start list");
			itemList = new ArrayList<Product>();
		}
		if (localName.equals("item")) {
			Log.d("start", "start item");
			currentProduct = new Product();
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		current = false;
		if (localName.equals("item")) {
			Log.d("start", "end item");
			if(currentProduct.getProductName().length()>1){
				itemList.add(currentProduct);
			}			
		} else if (localName.equals("productId")) {
			currentProduct.setProductId(currentValue);
		} else if (localName.equals("productName")) {
			currentProduct.setProductName(currentValue);
		} else if (localName.equals("merchId")) {
			currentProduct.setMerchId(currentValue);
		} else if (localName.equals("productMerchCatId")) {
			currentProduct.setProductMerchCatId(currentValue);
		} else if (localName.equals("productDesc")) {
			currentProduct.setProductDesc(currentValue);
		} 

		else if (localName.equals("price")) {
			currentProduct.setPrice(currentValue);
		} 
		else if (localName.equals("oldPrice")) {
			currentProduct.setOldPrice(currentValue);
		} 
		else if (localName.equals("productImageLink")) {
			currentProduct.setProductImageLink(currentValue);
		} 
		else if (localName.equals("productImageWidth")) {
			currentProduct.setProductImageWidth(currentValue);
		} 
		else if (localName.equals("productImageHeight")) {
			currentProduct.setProductImageHeight(currentValue);
		} 
		else if (localName.equals("productImage1")) {
			currentProduct.setProductImage1(currentValue);
		} 
		else if (localName.equals("productImage2")) {
			currentProduct.setProductImage2(currentValue);
		} 
		else if (localName.equals("productImage3")) {
			currentProduct.setProductImage3(currentValue);
		} 

	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub

		if (current) {
			currentValue = new String(ch, start, length);
			current = false;
		}
	}
}